package com.wlx.controller;


import com.wlx.anno.Log;
import com.wlx.pojo.Dept;
import com.wlx.pojo.Result;
import com.wlx.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    //private static Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptServiceImpl deptService;

    //@RequestMapping(value = "/depts",method = RequestMethod.GET)  //指定请求方式是get
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");

        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    //删除部门   @PathVariable 绑定路径参数
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门:{}",id);
        //调用service删除部门
        deptService.delete(id);

        return Result.success();
    }

    /*
    * 新增部门  @RequestBody 将json格式的数据封装到实体类对象中
    * */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);

        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("根据id查询部门数据:{}",id);

        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门数据:{}",dept);

        deptService.update(dept);

        return Result.success();
    }
}
